<template>
  <div class="app-container">
    <div style="position: relative">
      <div v-if="showData">
        <el-button-group style="">
          <el-button type="primary" :plain="plainA" @click="logtypeA">项目日志</el-button>
          <el-button type="primary" :plain="plainB" @click="logtypeB">个人日志</el-button>
        </el-button-group>
        <el-button-group style="position: absolute; right: 0">
          <el-button type="primary" :plain="plainC" @click="showDataBtnA">日历视图</el-button>
          <el-button type="primary" :plain="plainD" @click="showDataBtnB">台账视图</el-button>
        </el-button-group>
      </div>
      <div v-else>
        <el-button-group style="position: absolute; right: 0">
          <el-button type="primary" :plain="plainC" @click="showDataBtnA">日历视图</el-button>
          <el-button type="primary" :plain="plainD" @click="showDataBtnB">台账视图</el-button>
        </el-button-group>
      </div>
    </div>
    <!-- 日历 -->
    <el-calendar v-if="showData" v-model="value">
      <template slot="dateCell" slot-scope="{ data }">
        <div style="height: 100%; position: relative" @mouseover="onMouseOver(data)">
          {{ data.day.split("-").slice(1).join("-") }}
          <span v-if="arr.includes(data.day)">
            <el-tag effect="plain" type="success" style="
                border: 0;
                display: inline-block;
                position: absolute;
                right: 0;
              " color="rgb(225, 255, 219)">已填写</el-tag>
            <div style="width: 100%; text-align: center">
              <el-button style="
                  margin-top: -10px;
                  color: blue;
                  border: 0;
                  background: transparent;
                " size="medium" @click="handleUpdate(data)" icon="el-icon-postcard">查看详情</el-button>
            </div>
            <div style="text-align: center">{{ mapArr[data.day] }}</div>
          </span>
          <span v-else>
            <el-tag type="danger" style="
                border: 0;
                display: inline-block;
                position: absolute;
                right: 0;
              " color="rgb(255, 237, 235)" effect="plain">
              未填写
            </el-tag>
            <div style="width: 100%; text-align: center">
              <!-- data.day == hoverDate && -->
              <el-button v-if="data.day == hoverDate && compareDate(data.day, today)" @click="goAdd(data)"
                style="margin-top: 10px" type="primary" size="mini" icon="el-icon-plus">填写</el-button>
            </div>
          </span>
        </div>
      </template>
    </el-calendar>
    <!-- 台账 -->
    <div v-else>
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-form-item label="类型" prop="securityType">
          <el-select v-model="queryParams.securityType" placeholder="请选择类型" clearable>
            <el-option v-for="dict in dict.type.biz_security_type" :key="dict.value" :label="dict.label"
              :value="dict.value" />
          </el-select>
        </el-form-item>
        <el-form-item label="日志日期" prop="securityDate">
          <el-date-picker clearable v-model="queryParams.securityDate" type="daterange" value-format="yyyy-MM-dd"
            range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="安全员" prop="sercurityPeople">
        <el-select v-model="queryParams.sercurityPeople" placeholder="请选择安全员" clearable>
          <el-option v-for="(item, index) in sercurityPeopleOptions" :key="index" :value="item.userName"
            :label="item.nickName"></el-option>
        </el-select>
      </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
      <el-table :data="logList" @selection-change="handleSelectionChange">
        <el-table-column label="序号" align="center" prop="id" />
        <el-table-column label="类型" align="center" prop="securityType">
          <template slot-scope="scope">
            <dict-tag :options="dict.type.biz_security_type" :value="scope.row.securityType" />
          </template>
        </el-table-column>
        <el-table-column label="日志日期" align="center" prop="securityDate" width="180">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.securityDate, "{y}-{m}-{d}") }}</span>
          </template>
        </el-table-column>
        <el-table-column label="安全员" align="center" prop="sercurityPeopleName" />
        <el-table-column label="检查记录数" align="center" prop="checkRecordCount" />
        <el-table-column label="天气" align="center" prop="weather" />
        <el-table-column label="创建人" align="center" prop="createByName" />
        <el-table-column label="施工情况补录" align="center" prop="constructionSupplement" />
        <el-table-column label="特殊情况补录" align="center" prop="specialSupplement" />
        <el-table-column label="现场照片" align="center" prop="secnePhoto" width="100">
          <template slot-scope="scope">
            <image-preview :src="scope.row.secnePhoto" :width="50" :height="50" />
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
              v-hasPermi="['security:log:edit']">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
              v-hasPermi="['security:log:remove']">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>
  </div>
</template>
<script>
import {
  listLog,
  getLog,
  delLog,
  addLog,
  updateLog,
  listLogTime,
} from "@/api/biz/security/log";
import { getRevisePeople } from "@/api/biz/ledger/ledger";
import { mapState } from "vuex";
export default {
  name: "Log",
  dicts: ["biz_security_type"],
  data() {
    return {
    sercurityPeopleOptions: [],
      arr: [],
      mapArr: {},
      timeData: [],
      hoverDate: "",
      today: this.parseTime(new Date(), "{y}-{m}-{d}"),
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 安全日志表格数据
      logList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        securityType: '1',
        securityDate: null,
        sercurityPeople: null,
        checkRecordCount: null,
        weather: null,
        projectId: this.$store.state.app.selectData,
        constructionSupplement: null,
        specialSupplement: null,
        secnePhoto: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      value: new Date(),
      showData: true,
      weatcherData: {
        adcode: "",
        city: "",
        humidity: "",
        info: "",
        province: "",
        reportTime: "",
        temperature: 0,
        weather: "",
        windDirection: "",
        windPower: "",
      },
      plainA: false,
      plainB: true,
      plainC: false,
      plainD: true,
      securityType: "1",
    };
  },
  computed: {
    ...mapState(["app"]),
  },
  created() {
    this.getList();
  },
  watch: {
    $route: {
      handler(newVal, oldVal) {
        this.getList();
      },
      deep: true,
    },
    app: {
      handler(newVal, oldVal) {
        this.queryParams.projectId = newVal.selectData;
        this.getList();
      },
      deep: true,
    },
  },
  methods: {
    async getsercurityPeopleOptions() {
      const res1 = await getRevisePeople({
        projectId: this.queryParams.projectId,
        hiddenTrouble: "0",
      })
      this.sercurityPeopleOptions = res1.rows;
    },
    /** 查询安全日志列表 */
    getList() {
      if (this.queryParams.projectId) {
        // this.loading = true;
      listLog(this.queryParams).then((response) => {
        this.logList = response.rows;
        this.total = response.total;
      });
      listLogTime({...this.queryParams, pageNum: null, pageSize: null, securityType: this.securityType}).then((response) => {
        this.timeData = response.rows;
        this.arr = this.timeData.map((item) => item.securityDate);
        this.timeData.forEach((item) => {
          this.mapArr[item.securityDate] = item.event;
        });
      });
      this.getsercurityPeopleOptions();
      }
      // this.loading = false;
    },
    compareDate(date, today) {
      return new Date(date).getTime() <= new Date(today).getTime();
    },
    //鼠标悬停按钮
    onMouseOver(data) {
      this.hoverDate = data.day;
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        securityType: null,
        securityDate: null,
        sercurityPeople: null,
        checkRecordCount: null,
        weather: null,
        projectId: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        constructionSupplement: null,
        specialSupplement: null,
        secnePhoto: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加安全日志";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      if (row.day != null) {
        this.timeData.forEach((item) => {
          if (item.securityDate == row.day) {
            let id = item.id;
            this.$router.push({
              name: "addLog",
              query: { id: id },
            });
          }
        });
      } else {
        this.reset();
        const id = row.id || this.ids;
        this.$router.push({
          name: "addLog",
          query: { id: id },
        });
      }
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除安全日志编号为"' + ids + '"的数据项？')
        .then(function () {
          return delLog(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "security/log/export",
        {
          ...this.queryParams,
        },
        `log_${new Date().getTime()}.xlsx`
      );
    },
    onClacking() {
      this.showData = !this.showData;
    },
    goAdd(data) {
      this.$router.push({
        name: "addLog",
        query: { day: data.day, securityType: this.securityType },
      });
    },
    logtypeA() {
      this.securityType = "1";
      this.plainA = false;
      this.plainB = true;
      if (this.queryParams.projectId) {
        listLogTime({...this.queryParams, pageNum: null, pageSize: null, securityType: this.securityType}).then((response) => {
        this.timeData = [];
        this.arr = [];
        this.mapArr = {};
        this.timeData = response.rows;
        this.arr = this.timeData.map((item) => item.securityDate);
        this.timeData.forEach((item) => {
          this.mapArr[item.securityDate] = item.event;
        });
      });
      }else{
        this.$message({
          message: '请选择项目部后再点击！',
          type: 'warning'
        });
      }
    },
    logtypeB() {
      this.securityType = "2";
      this.plainA = true;
      this.plainB = false;
      if (this.queryParams.projectId) {
        listLogTime({...this.queryParams, pageNum: null, pageSize: null, securityType: this.securityType}).then((response) => {
        this.timeData = [];
        this.arr = [];
        this.mapArr = {};
        this.timeData = response.rows;
        this.arr = this.timeData.map((item) => item.securityDate);
        this.timeData.forEach((item) => {
          this.mapArr[item.securityDate] = item.event;
        });
      });
      }else{
this.$message({
          message: '请选择项目部后再点击！',
          type: 'warning'
        });
      }
    },
    showDataBtnA() {
      this.showData = true;
      this.plainC = false;
      this.plainD = true;
      this.logtypeA();
    },
    showDataBtnB() {
      this.showData = false;
      this.plainC = true;
      this.plainD = false;
    },
  },
};
</script>
